using System;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
    /// <summary>
    /// A <see cref="IBusinessObjectRule"/> that validates that the Multiple side of a One-To-Many <see cref="BO.DMRelationship"/>
    /// has the composition relationship>.
    /// </summary>
    public class DMRelationshipManyToOneMustBeAssociation : IBusinessObjectRule
    {
        public IDMRelationship DMRelationship { get; private set; }

//        public DMRelationshipManyToOneMustBeAssociation(IDMRelationship dmRelationship)
//        {
//            if (dmRelationship == null) throw new ArgumentNullException("dmRelationship");
//            DMRelationship = dmRelationship;
//        }

        #region Implementation of IBusinessObjectRule

        /// <summary>
        /// Indicates whether the property value is valid against the rules
        /// </summary>
        /// <returns>
        /// Returns true if valid
        /// </returns>
        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            DMRelationship = (IDMRelationship) bo;
            if (!DMRelationship.IsManyToOne) return true;
            return DMRelationship.IsAssociation;
        }

        /// <summary>
        /// Returns the rule name
        /// </summary>
        public string Name
        {
            get { return "DMRelationshipManyToOneMustBeAssociation"; }
        }

        /// <summary>
        /// Returns the error message for if the rule fails.
        /// </summary>
        public string Message
        {
            get
            {
                return "The Many to One " + DMRelationship.RelationshipType + " Relationship '" 
                        + DMRelationship.RelationshipName +"' on class '" + DMRelationship.OwnerClassName +
                        "' must be Association";
            }
        }

        /// <summary>
        /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
        /// </summary>
        public ErrorLevel ErrorLevel
        {
            get { return Habanero.Base.ErrorLevel.Error; }
        }

        #endregion
    }
}